package com.toman.ui.work.scan;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.toman.Interfaces.NewBaseUrl;
import com.toman.JavaBean.DeviceEntity;
import com.toman.R;
import com.toman.Util.IntentUtils;
import com.toman.Util.ToastUtil;
import com.toman.Util.ViewUtil;
import com.toman.toolbar.LoadingEmptyActivity;
import com.toman.ui.work.base.DeviceInfActivity;
import com.toman.ui.work.device.CompleteEquipmentAddActivity;
import com.toman.ui.work.device.DeviceRepairAddActivity;
import com.toman.ui.work.device.PreMaintenanceAddActivity;
import com.toman.ui.work.device.TallyAddActivity;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Request;
import okhttp3.Response;

public class ScanDeviceEmptyActivity extends LoadingEmptyActivity {

    @BindView(R.id.btn_base)
    Button btnBase;
    @BindView(R.id.btn_tally)
    Button btnTally;
    @BindView(R.id.btn_repair)
    Button btnRepair;
    @BindView(R.id.btn_preview)
    Button btnPreview;
    @BindView(R.id.btn_check)
    Button btnCheck;
    DeviceEntity bean;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scan_device);
        ButterKnife.bind(this);
        initToolBar("扫一扫");
        initView();
        get();
    }

    @Override
    protected Activity getContent() {
        return this;
    }

    @Override
    public void load() {
finish();
    }

    private void initView() {
//        获取设备编号
        id = getIntent().getStringExtra("id");
    }

    @OnClick({R.id.btn_base, R.id.btn_tally, R.id.btn_repair, R.id.btn_preview, R.id.btn_check})
    public void onViewClicked(View view) {

        switch (view.getId()) {
            case R.id.btn_base:
                Bundle bundle=new Bundle();
                bundle.putString("id",bean.getId()+"");
                IntentUtils.startActivity(this, DeviceInfActivity.class,bundle);
                break;
            case R.id.btn_tally:
                 bundle=new Bundle();

                bundle.putSerializable("bean",bean);
                IntentUtils.startActivity(this, TallyAddActivity.class,bundle);
                break;
            case R.id.btn_repair:
                bundle=new Bundle();
                bundle.putString("id",bean.getDepartmentId()+"");
                bundle.putString("deviceId",bean.getId()+"");
                bundle.putString("status","2");

                bundle.putString("deviceName",bean.getDeviceName());
                IntentUtils.startActivity(this, DeviceRepairAddActivity.class,bundle);
                break;
            case R.id.btn_preview:
                bundle=new Bundle();
                bundle.putString("id",bean.getDepartmentId()+"");
                bundle.putString("device_id",bean.getId()+"");
                bundle.putString("device_name",bean.getDeviceName());
                IntentUtils.startActivity(this, PreMaintenanceAddActivity.class,bundle);
                break;
            case R.id.btn_check:
                bundle=new Bundle();
                bundle.putString("id",bean.getDepartmentId()+"");
                bundle.putString("device_id",bean.getId()+"");
                bundle.putString("device_name",bean.getDeviceName());
                IntentUtils.startActivity(this, CompleteEquipmentAddActivity.class,bundle);
                break;
        }
    }
    public void get() {
        OkHttpUtils
                .get(NewBaseUrl.device_info_get + id)
                .params("token", token)
                .params("id", id)
                .execute(new StringCallback() {
                    @Override
                    public void onResponse(boolean isFromCache, String result, Request request, @Nullable Response response) {
                        Log.i("test", "设备详情: " + result);

                        JSONObject jsonObj = JSON.parseObject(result);
                        int code = jsonObj.getInteger("code");ViewUtil.lagout(code,getApplicationContext());
                        if (code == 0) {
                             bean = (DeviceEntity) JSON.parseObject(jsonObj.getString("device"), DeviceEntity.class);






                        } else {
                            ToastUtil.makeText(getApplicationContext(), jsonObj.getString("msg"));
                            finish();
                        }

                    }
                });
    }
}
